package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.TerPosition;
import com.tbit.main.service.HistoryService;
import com.tbit.main.service.MachineService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 历史轨迹
 *
 * @author Leon
 * 2019年11月11日 下午10:00:01
 */
@RestController
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private HistoryService historyService;
    @Autowired
    private MachineService machineService;

    /**
     * 根据时间查询
     */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(String token, String userCode, String startTime, String endTime, Integer mapType) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        List<TerPosition> terPositions = historyService.getByMachineNOAndTime(mapType, machine.getMachineNO(), startTime, endTime,machine.getMachineId());

        return JsonResult.succ(terPositions);
    }

    /**
     * 获取最后一次骑行轨迹
     */
    @RequestMapping("/getLastRideHistory")
    public JsonResult getLastRideHistory(String token, String userCode, Integer mapType) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        List<TerPosition> terPositions = historyService.getLastRideHistory(mapType, machine.getMachineNO());

        return JsonResult.succ(terPositions);
    }

    /**
     * 获取最后非用户骑行轨迹
     */
    @RequestMapping("/getLastActiveHistory")
    public JsonResult getLastActiveHistory(String token, String userCode, Integer mapType) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        List<TerPosition> terPositions = historyService.getLastActiveHistory(mapType, machine.getMachineNO());

        return JsonResult.succ(terPositions);
    }


}